701A - Cards - CodeForces Solution


greedy implementation *800

Please click on ads to support us..

Python Code:

card_number = int(input())
card_values = list(map(int, input().split() ))
sorted_card_values = sorted(card_values)
answer_list = []
for i in range(card_number//2):
    answer_list.append(str(card_values.index(sorted_card_values[i])+1))
    card_values[card_values.index(sorted_card_values[i])] = 0
    answer_list.append(str(card_values.index(sorted_card_values[-(i+1)])+1))
    card_values[card_values.index(sorted_card_values[-(i+1)])] = 0
    print(" ".join(answer_list))
    answer_list.clear()

C++ Code:

// Problem: A. Cards
// Contest: Codeforces - Codeforces Round #364 (Div. 2)
// URL: https://codeforces.com/contest/701/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms

/*
	Author: MikiMiku
	
	Observation:
	
	Idea: 
		
*/

#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define endl '\n'

#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back 
#define eb emplace_back 
#define mp make_pair

#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define LSB(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define modulo(S, N) ((S) & (N - 1))   // returns S % N, where N is a power of 2
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))

typedef complex<double> point;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const int FMOD = 998244353; 
const ll INF = 1e9;
const ld EPS = 1e-9;
const double PI=acos(-1);

#define fi first
#define se second
typedef pair<int, int> ii;  
typedef vector<ii> vii;
typedef vector<int> vi;
typedef map<int,int> mii; 

//FLOAT PRECISION SETTINGS
/*
cout.setf(ios::fixed,ios::floatfield);
cout.precision(3);
*/
//FILE IO
void setIO(string s) {
	freopen((s + ".in").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}

//........................................................................

signed main() {
	ios_base::sync_with_stdio(0);cin.tie(nullptr); cout.tie(nullptr);
	int n;
	cin >> n;
	
	map<int, vector<int>> mi;
	int sum = 0;
	for(int i = 0; i < n; ++i) {
		int ai;
		cin >> ai; sum += ai;
		mi[ai].pb(i);
	}
	
	sum /= (n/2);
	while(!mi.empty()) {
		auto it = mi.begin();
		int cur = (*it).fi;
		
		int id1 = mi[cur].back(); mi[cur].pop_back(); if(sza(mi[cur]) == 0) mi.erase(cur);
		int id2 = mi[sum - cur].back(); mi[sum - cur].pop_back(); if(sza(mi[sum - cur]) == 0) mi.erase(sum - cur);
		
		cout << id1 + 1 << " " << id2 + 1 << endl;
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST